%% Copyright 2008 Kevin A. Smith 
%% Licensed under the Apache License, Version 2.0 (the "License"); 
%% you may not use this file except in compliance with the License. 
%% You may obtain a copy of the License at 
%%
%% http://www.apache.org/licenses/LICENSE-2.0 
%%
%% Unless required by applicable law or agreed to in writing, software 
%% distributed under the License is distributed on an "AS IS" BASIS, 
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
%% See the License for the specific language governing permissions and 
%% limitations under the License.

-module(run_instance_parser).

-compile([export_all]).

callback({startElement, _, "item", _, _}, {_Current, _Flag, All}) ->
	{[], [], All};
callback({startElement, _, "instanceId", _, _}, {Current, _Flag, All}) ->
	{Current, instanceId, All};
callback({endElement, _, "item", _}, {Current, _Flag, All}) ->
	{[], [], lists:append(All, [Current])};
callback({startElement, _, "code", _, _}, {Current, _Flag, All}) ->
	{Current, instanceStateCode, All};
callback({startElement, _, "name", _, _}, {Current, _Flag, All}) ->
	{Current, instanceStateName, All};
callback({startElement, _, "InstanceType", _, _}, {Current, _Flag, All}) ->
	{Current, instanceType, All};
callback({startElement, _, "launchTime", _, _}, {Current, _Flag, All}) ->
	{Current, launchTime, All};
callback({endElement, _, _, _}, {Current, Flag, All}) when is_atom(Flag)->
	{Current, [], All};
callback(endDocument, {_Current, _Flag, All}) ->
	{ok, lists:filter(fun drop_empty/1, All)};		
callback({characters, Text}, {Current, Flag, All}) when is_atom(Flag) ->
    Current1 = lists:append(Current, [{Flag, Text}]),
    {Current1, Flag, All};
callback(_Event, State) ->
    State.

drop_empty(I) ->
	length(I) > 0.